/**
 * Created by tangqiaojie <2482366539@qq.com> on 2020/4/7 0007
 */
import { insertMoment, nextTick } from '../../lib/utils.js';
import { sendSbList, findIndexButton } from './tool.js';
import { getTables } from './asyncTable.js';

let currentZeroInfo;

const handleSmallIndex = ()=>{
  nextTick(()=>{
    var btn = $('#cygn a:eq(0)');
    if (btn.length) {
      btn.click();
      return true;
    }
  }, 1000);
};

const handleSmallStep3 = (currentZeroInfo)=>{
  insertMoment();
  nextTick(()=>{
    if ($('#frmMain').length && $('#frmMain').contents().find('#frmSheet').length) {
      let dom = $('#frmMain').contents().find('#frmSheet').contents()
        .find("[ng-model='fjsnsrxxForm.zjhm']");
      let txt = $('#frmMain').contents().find('#frmSheet')[0].contentWindow.$("[ng-model='fjssbInitData.dlrsfzhXs']");
      if (dom.length && txt.length && dom.val() != '') {
        let name = dom.val();
        txt.focus();
        txt.val(name);
        txt.blur();
        window.setTimeout(()=>{
          window.frames[0].prepareMake(true);
          nextTick(()=>{
            let btn = $('#frmMain').contents().find('.layui-layer-btn0');
            if (btn.length) {
              $('#frmMain')[0].contentWindow.$('.layui-layer-btn0').click();
              nextTick(()=>{
                let btn = $('#layui-layer3 .layui-layer-btn0');
                if (btn.length) {
                  btn.click();
                  nextTick(()=>{
                    let result = $('#frmMain').contents().find('h3.font-green');
                    if (result.length) {
                      /* sendSbList(currentZeroInfo);
                      setTimeout(()=>{
                        window.sessionStorage.clear();
                        window.location.href="http://etax.beijing.chinatax.gov.cn/sso/logout?service=http%3A%2F%2Fetax.beijing.chinatax.gov.cn%2Fxxmh";
                      },2000); */
                      window.close();
                      return true;
                    }
                  });
                }
              }, 1000);
              return true;
            }
          }, 500);
        }, 500);
        return true;
      }
    }
  }, 5000);
};

let maskNone = function () {
  let maskFrmSheet = $('body > div[id="maskFrmSheet"]');
  return Array.from(maskFrmSheet).every(
    (item) => $(item).css('display') === 'none'
  );
};

const handleSmallStep2 = ()=>{
  $('#frmMain').contents().find('.layui-layer-btn0')[0].click();
  nextTick(()=>{
    let frmMain = $('#frmMain');
    if (frmMain.contents().find('.layui-layer-btn0').length) {
      frmMain.contents().find('.layui-layer-btn0')[0].click();
    }
    let win = $('#frmMain')[0].contentWindow;
    if (frmMain.length && $('#btnPrepareMake').length && !$('#layui-layer-shade2').length && maskNone()) {
      if (frmMain.contents().find('#fyyqfkgz').length) {
        frmMain.contents().find('#fyyqfkgz')[0].click();
      }
      $('#btnPrepareMake').click();
      nextTick(()=>{
        if ($('.layui-layer-dialog').length) {
          $('.layui-layer-dialog .layui-layer-btn0').click();
          nextTick(()=>{
            if ($('.layui-layer-dialog').length) {
              $('.layui-layer-dialog .layui-layer-btn0').click();
              nextTick(()=>{
                let content = $('#frmMain').contents().find('.layui-layer-content');
                if ($('#frmMain')[0].contentWindow && $('#frmMain')[0].contentWindow.yesHandler) {
                  $('#frmMain')[0].contentWindow.yesHandler();
                  return true;
                }
              }, 10000);
              return true;
            }
          }, 1000);
          return true;
        }
      }, 1000);
      return true;
    }
  }, 3000);
};

const handleSmallStep1 = (currentZeroInfo)=>{
  nextTick(()=>{
    if ($('#ifrMain').length && $('#ifrMain').contents().find('#lhsbIframe').length) {
      let win = $('#ifrMain').contents().find('#lhsbIframe')[0].contentWindow;
      let zzsObj = findIndexButton('增值税(适用于小规模纳税人)', win);
      let incomeObj = findIndexButton('企业所得税(月季报)', win);
      let zzsClicked = sessionStorage.getItem('zzsClicked'),
        incomeClicked = sessionStorage.getItem('incomeClicked');
      if (zzsObj.txt === '填写申报表' && !zzsClicked) {
        let str = $('#ifrMain').contents().find('#lhsbIframe').contents()
          .find('#gdslhsb tr:eq(1) .layui-btn:eq(0)')
          .attr('href');
        str = str.replace('javaScript:', '');
        $('#ifrMain').contents().find('#lhsbIframe')[0].contentWindow.eval(str);
        nextTick(()=>{
          if ($('#ifrMain').contents().find('#layui-layer2 .layui-btn-xs:eq(1)').length) {
            let str = $('#ifrMain').contents().find('#layui-layer2 .layui-btn-xs:eq(1)').attr('onclick');
            str = str.replace('javaScript:', '');
            sessionStorage.setItem('zzsClicked', 1);
            $('#ifrMain')[0].contentWindow.eval(str);
            return true;
          }
        }, 1000);
      }
      if (incomeObj.txt === '填写申报表' && !incomeClicked) {
        sessionStorage.setItem('incomeClicked', 1);
        let str = incomeObj.btn.attr('href');
        str = str.replace('javaScript:', '');
        win.eval(str);
      }

      const checkStatus = ()=>{
        if (zzsObj.refresh) {
          let str1 = zzsObj.refresh.attr('href');
          str1 = str1.replace('javaScript:', '');
          win.eval(str1);
        }
        if (incomeObj.refresh) {
          let str2 = incomeObj.refresh.attr('href');
          str2 = str2.replace('javaScript:', '');
          win.eval(str2);
        }
        setTimeout(()=>{
          window.location.reload();
        }, 10000);
      };

      if (zzsObj.txt === '已申报' && incomeObj.txt === '已申报') {
        insertMoment();
        nextTick(()=>{
          if (window.moment) {
            sendSbList(currentZeroInfo, win);
            setTimeout(()=>{
              getTables();
            }, 1000);
            return true;
          }
        });
      } else {
        setInterval(()=>{
          checkStatus();
        }, 20000);
      }


      return true;
    }
  }, 2000);
};


export {
  handleSmallIndex,
  handleSmallStep1,
  handleSmallStep2,
  handleSmallStep3
};
